package org.groovymud.utils;

/* Copyright 2008 Matthew Corby-Eaglen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.groovymud.engine.event.EventScope;
import org.groovymud.engine.event.ScopedEvent;
import org.groovymud.engine.event.messages.MessageEvent;
import org.groovymud.engine.event.observer.Observable;
import org.groovymud.object.Container;
import org.groovymud.object.MudObject;
import org.groovymud.object.alive.Alive;
import org.groovymud.shell.telnetd.ExtendedTerminalIO;

/**
 * a statically accessed (spit) set of messenger utils
 * 
 * sends messages to various scopes
 * 
 * sending a message to global will send it to everyone (eventually) sending the
 * message to room will send it to the room contents (and below) sending it to
 * player scope will keep the message private to the player
 * 
 * @author matt
 * 
 */
public class MessengerUtils {

	private static final Logger logger = Logger.getLogger(MessengerUtils.class);

	public static void sendMessageToPlayer(Observable source, String sourceMessage) {
		fireMessage(EventScope.PLAYER_SCOPE, source, sourceMessage, null);
	}

	public static void sendMessageToRoom(Observable source, String sourceMessage, String roomMessage) {
		fireMessage(EventScope.ROOM_SCOPE, source, sourceMessage, roomMessage);
	}

	public static void sendMessageToMud(Observable source, String sourceMessage, String roomMessage) {
		fireMessage(EventScope.GLOBAL_SCOPE, source, sourceMessage, roomMessage);
	}

	public static void sendMessageToPlayers(Observable source, String sourceMessage, String targetMessage, MudObject... target) {
		fireMessage(EventScope.GLOBAL_SCOPE, source, sourceMessage, targetMessage, target);
	}

	public static void fireMessage(EventScope scope, Observable source, String sourceMessage, String roomMessage, MudObject... targets) {
		MessageEvent msgEvent = new MessageEvent(scope);
		msgEvent.setSourceMessage(sourceMessage);
		if (targets != null && targets.length > 0) {
			msgEvent.setTargets(Arrays.asList(targets));
			msgEvent.setTargetMessage(roomMessage);
		} else {
			msgEvent.setScopeMessage(roomMessage);
		}
		if (source != null) {
			source.fireEvent(msgEvent);
		} else {
			logger.info(msgEvent);
		}
	}

	public static void writeMessage(Container container, ScopedEvent arg) {
		MessageEvent mEvent = (MessageEvent) arg;
		Map<String, Set<MudObject>> everyone = container.getInventoryHandler().getMudObjectsMap(true);
		Iterator<Set<MudObject>> all = everyone.values().iterator();

		while (all.hasNext()) {
			Set<MudObject> nextSet = all.next();
			Iterator<MudObject> setIterator = nextSet.iterator();
			while (setIterator.hasNext()) {
				Alive obj = (Alive) setIterator.next();
				ExtendedTerminalIO term = obj.getTerminalOutput();
				if (term == null) {
					continue;
				}
				if (mEvent.getSource() == obj) {
					try {
						term.writeln(mEvent.getSourceMessage());
					} catch (IOException e) {
						logger.error(e, e);
					}
				} else {
					try {
						if (mEvent.getScopeMessage() != null) {
							term.writeln(mEvent.getScopeMessage());
						}
						if (mEvent.getTargets().contains(obj)) {
							term.writeln(mEvent.getTargetMessage());
						}
					} catch (IOException e) {
						logger.error(e, e);
					}
				}
			}
		}

	}
}
